/*
 * Copyright 2013-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tinem.platform.web.gateway.filter;

import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.pojo.co.RedisKeyEnum;
import com.tinem.platform.module.pojo.enums.security.SignatureAlgorithm;
import com.tinem.platform.web.gateway.util.GatewayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className ChannelScopeGatewayFilterFactory
 * @description 客户端返回数据加签
 * @site
 * @company
 * @create 2020-07-03 12:26
 */
@Slf4j
@Component
public class ClientResSignGatewayFilterFactory extends ModifyResponseBodyGatewayFilterFactory {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public GatewayFilter apply(ModifyResponseBodyGatewayFilterFactory.Config c) {
        ModifyResponseBodyGatewayFilterFactory.Config config = new ModifyResponseBodyGatewayFilterFactory.Config();
        config.setRewriteFunction(String.class,String.class,(exchange,resBody)-> {

            String client = GatewayUtil.getClient(exchange);
            String clientRedisKey = RedisKeyEnum.client_info.getKey(client);

            // 缓存中客户端返回是否加签
            String  resSign = (String) stringRedisTemplate.opsForHash().get(clientRedisKey, "resSign");
            if(!Boolean.parseBoolean(resSign)){
                return Mono.just(resBody);
            }

            // 缓存中返回的加密类型
            String resSignType = (String) stringRedisTemplate.opsForHash().get(clientRedisKey, "resSignType");
            // 缓存中返回加密KEY
            String resSignKey = (String) stringRedisTemplate.opsForHash().get(clientRedisKey, "resSignKey");

            String resTimestamp = exchange.getResponse().getHeaders().getFirst(GatewayHeadName.X_PLATFORM_GATEWAY_RES_TIMESTAMP);

            String apiRequestId = GatewayUtil.getApiReq(exchange);

            String sign = SignatureAlgorithm.valueOf(resSignType).sign(resBody,resSignKey,resTimestamp,apiRequestId);
            exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_SIGN,sign);
            exchange.getResponse().getHeaders().set(GatewayHeadName.X_PLATFORM_GATEWAY_RES_SIGN_TYPE,resSignType);

            return Mono.just(resBody);
        });
        return super.apply(config);
    }

}
